package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * Connects to the database and deletes a row from either the a la carte menu or
 * lunch menu tables.
 * 
 * 
 * @author Ludvig Gee
 * 
 */

public class DeleteDb {
    Connection con;
    String restID, dish, option;
    String delete;

    public DeleteDb() {

    }

    public DeleteDb(String restID) {

	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    System.out.println(e);
	}
	try {
	    con = DriverManager.getConnection(
		    "jdbc:mysql://db.student.chalmers.se/", "olofek",
		    "Ac6Jkdoa");
	} catch (Exception e) {
	    System.out.println(e);
	    System.out.println("bajs");
	}

    }

    /**
     * Executes deletion of row in database.
     * 
     * @param restID
     *            ID of current owner logged in as string.
     * 
     * @param dish
     *            Name of dish as string that is to be deleted.
     * 
     * @param option
     *            String that determines which query to execute.
     * 
     */
    public void DeleteAction(String restID, String dish, String option) {

	if (option.equals("1")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "DELETE FROM olofek.menu WHERE menu.rest_id='"
			+ restID + "' AND menu.dish='" + dish + "'";
		delete = "delDone";
		stmt.executeUpdate(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}

	if (option.equals("3")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "DELETE FROM olofek.lunch WHERE lunch.rest_id='"
			+ restID + "' AND lunch.dish='" + dish + "'";
		delete = "delDone";
		stmt.executeUpdate(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}

    }

    /**
     * 
     * @return ID of owner who made query.
     */
    public String getRestID() {
	return restID;
    }

    /**
     * parameter to signify successful deletion.
     * 
     * @return string set depending if query is made.
     */
    public String getDeleteDone() {
	return delete;
    }
}
